import babelpolyfill from 'babel-polyfill'
import Vue from 'vue'
import App from './App'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
//import './assets/theme/theme-green/index.css'
import VueRouter from 'vue-router'
import store from './vuex/store'
import Vuex from 'vuex'
//import NProgress from 'nprogress'
//import 'nprogress/nprogress.css'
import routes from './routes'

import xxx from './common/js/permission'

import 'font-awesome/css/font-awesome.min.css'

Vue.use(ElementUI)
Vue.use(VueRouter)
Vue.use(Vuex)

//NProgress.configure({ showSpinner: false });

// 引入axios
import axios from "axios";
// 配置axios的基础路径
axios.defaults.baseURL = "http://localhost:8080/"
// 配置vue的axios
Vue.prototype.$http = axios;

// 引入百度地图
import BaiduMap from 'vue-baidu-map'
Vue.use(BaiduMap, {
  // Visit http://lbsyun.baidu.com/apiconsole/key for details about app key.
  ak: 'PQ9FAt6qg7taDWj6LLABYO7u6bSETXhD'
})

const router = new VueRouter({
  routes
})

//========================= axios前置拦截器 ==================================//
// 发送axios请求之前都会执行以下的代码
axios.interceptors.request.use(res =>{
  // 从localstorage中获取token
  var token = localStorage.getItem("token");
  if(token){ // 如果token有值
    // 携带在请求头发送到后端 - 将token设置到请求头
    res.headers["token"] = token;
  }
  return res;
},error => {
  // 请求拦截
  Promise.reject(error);
})
//========================= axios前置拦截器 ==================================//


//========================= axios后置拦截器 ==================================//
// 发送axios请求之后都会执行以下的代码
axios.interceptors.response.use(res =>{
  if(false === res.data.success && "noLogin" === res.data.message){
    localStorage.removeItem("token");
    localStorage.removeItem("logininfo");
    localStorage.removeItem("permissions");
    localStorage.removeItem("menus");
    // 跳转到登录页面
    router.push({ path: '/login' });
  }else if(false === res.data.success && "noPermission" === res.data.message){
      alert("你没有权限!!!");
  }else if(false === res.data.success && "timeout" === res.data.message){
    localStorage.removeItem("token");
    localStorage.removeItem("logininfo");
    localStorage.removeItem("permissions");
    localStorage.removeItem("menus");
     alert("jwt过期了!!!");
  }
  return res;
},error => {
  // 请求拦截
  Promise.reject(error);
})
//========================= axios后置拦截器 ==================================//


//========================= 路由的前置拦截器 ==================================//
//前端的登录拦截器 - 路由的前置拦截器
router.beforeEach((to, from, next) => {
  // 如果是登录或注册请求，直接放行
  if (to.path == '/login' || to.path == '/register' || to.path == '/active/') {
    // 放行
    next();
  }else {
    let logininfo = JSON.parse(localStorage.getItem('logininfo'));
    if(logininfo){
      // 放行
      next();
    }else {
      // 跳转登录页面
      router.push({ path: '/login' });
    }
  }
})

//处理页面刷新动态路由失效问题
initIndexRouters();
export function initIndexRouters(){
  if(!localStorage.menus){
    return;
  }
  //防止重复配置路由
  if(router.options.routes.length>6){
    location.reload()
    return;
  }
  let menus = localStorage.getItem('menus');
  menus = JSON.parse(menus);
  let tempRouters = [];
  menus.forEach(menu=>{
    let indexRouter = {
      path: '/',
      iconCls: menu.icon,
      name: menu.name,
      component: resolve => require(['@/views/Home'], resolve),
      children: []
    }
    menu.children.forEach(cMenu=>{
      let cr = {
        path: cMenu.url,
        name: cMenu.name,
        component: resolve => require(['@/views/'+cMenu.component], resolve)
      }
      indexRouter.children.push(cr)
    })
    tempRouters.push(indexRouter)
    router.options.routes.push(indexRouter)
  })
  //动态路由配置
  router.addRoutes(tempRouters);
}
// 路由后置拦截器：每一次通过路由的请求之后都会执行以下的逻辑
// router.afterEach(transition => {
//NProgress.done();
//});

new Vue({
  //el: '#app',
  //template: '<App/>',
  router,
  store,
  //components: { App }
  render: h => h(App)
}).$mount('#app')



